package model.feature;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import model.Settings;
import model.inventory.FieldFormatMapper;
import util.PlayerUtilities;

public class FeatureDefinition {
	private static FeatureDefinition _instance;
	private List<FeatureContainerDefinition> definition;
	private FieldFormatMapper textFormatMapper;
	
	public synchronized static FeatureDefinition getInstance() {
		// load inventory player from XML file if the data file exists.
		if (_instance == null) {
			File definitionFile = new File(Settings.FEATURE_DEF_XML);
			if (definitionFile.exists()) {
				Object obj = PlayerUtilities.getObjectFromXMLFile(Settings.FEATURE_DEF_XML);
				if (obj instanceof FeatureDefinition) {
					_instance = (FeatureDefinition) obj;
					return _instance;
				}
			}
			_instance = new FeatureDefinition();
		}
		return _instance;
	}

	public void saveToXML() {
		PlayerUtilities.saveObjectToXMLFile(Settings.FEATURE_DEF_XML, this);
	}
	
	public void saveModifiedXML(FeatureDefinition obj)
	{
		PlayerUtilities.saveObjectToXMLFile(Settings.FEATURE_DEF_XML_TEST, obj);
	}

	private FeatureDefinition() {
		this.reset();
		this.textFormatMapper = new FieldFormatMapper();
	}
	
	public boolean deleteDefinitionByID(int[] id)
	{
		boolean isDeleted = false;
		if(definition == null)
			return isDeleted;
		
		if(id.length>0)
		{
			for(int i : id)
			{
				System.out.println("FEATURE DEF DEL ID "+i);
				
				if(definition.size()>0)
				{
					for(FeatureContainerDefinition feature : definition)
					{
						if(feature.getFeatureId() == i)
						{
							System.out.println("DELETE FEATURE ID"+i);
							definition.remove(feature);
							isDeleted = true;
							PlayerUtilities.setUpdateStringJson("FEATURE_DEF_DEL",i);
							break;
						}
					}
				}
			}
		}
		
		
		
		if(isDeleted)
		{
			if(definition != null)
			{
				if(definition.size()>0)
				{
					for(FeatureContainerDefinition feature : definition)
					{
						System.out.println("FEATURE TYPE IS +++++++++++ "+feature.getFeatureType());
					}
				}
			}
			
			System.out.println("SAVING TO FILE");
			this.setDefinition(definition);
			
			PlayerUtilities.saveObjectToXMLFile(Settings.FEATURE_DEF_XML, this);
			/*try
			{
				new File(Settings.FEATURE_DEF_XML).delete();
				FileUtils.copyFile(new File(Settings.FEATURE_DEF_XML_TEST), new File(Settings.FEATURE_DEF_XML));
			}
			catch(Exception e)
			{
				
			}*/
			
		}
		System.out.println(isDeleted);
		return isDeleted;
	}

	public FeatureContainerDefinition getFeatureWiseDefinition(String featureType)
	{	
		FeatureContainerDefinition returunObj = null;
		
		if(definition.size()>0)
		{
			for(FeatureContainerDefinition feature : definition)
			{
				if(feature.getFeatureType().equals(featureType))
				{
					returunObj = feature;
					break;
				}
			}
		}
		return returunObj;
	}
	
	public List<Integer> getFeatureContainerIDs()
	{
		List<Integer> IDS = new ArrayList<Integer>();
		
		if(definition.size()>0)
		{
			for(FeatureContainerDefinition feature : definition)
			{
				IDS.add(feature.getContainerID());
			}
		}
		return IDS;
	}
	
	public void reset() {
		//System.out.println("Reseting features definition");
		//definition.clear();
	}

	public FieldFormatMapper getTextFormatMapper() {
		return textFormatMapper;
	}

	public void setTextFormatMapper(FieldFormatMapper textFormatMapper) {
		this.textFormatMapper = textFormatMapper;
	}
	
	
	public List<FeatureContainerDefinition> getDefinition() {
		return definition;
	}

	public void setDefinition(List<FeatureContainerDefinition> definition) {
		this.definition = definition;
	}

	public static void main(String[] args) {
		FeatureDefinition obj = FeatureDefinition.getInstance();
		
		int[] idlist = {14};
		
		obj.deleteDefinitionByID(idlist);
		
		//System.out.println(obj.getTextFormatMapper().get("option1"));
		/*
		FeatureContainerDefinition object = obj.getFeatureWiseDefinition("WEATHER");
		
		if(object !=null)
		{
			System.out.println(object.getFeatureType());
		}
		
		List<Integer> list = obj.getFeatureContainerIDs();
		
		for(int a : list)
		{
			System.out.println(a);
		}
		
		List<FeatureContainerDefinition> containerDefnition = obj.getDefinition();
		
		for(FeatureContainerDefinition a : containerDefnition)
		{
			System.out.println(a.getNumberOfDaysToPlay());
		}
		
		*/
		
	}
	
}
